﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.Data;
using Krile.Kernel.Bridges;
using System.Runtime.InteropServices;

namespace Krile.Forms.MainForm
{
    public partial class Popup : Form, IFormEx
    {
        IFormEx parentForm;

        public Popup(IFormEx parent)
        {
            parentForm = parent;
            InitializeComponent();
            applySd = null;
            surface.OnSelectedItemChanged += new Action(surface_OnSelectedItemChanged);
        }

        void surface_OnSelectedItemChanged()
        {
            if (surface.Statuses != null && surface.SelectedIndex != 0)
            {
                this.Visible = false;
            }
        }

        SearchDataBase applySd;

        public Popup(IFormEx parent,ulong uid)
            : this(parent,new Data.UIDSearchData() { UID = uid, Exclude = false }, true)
        { }

        public Popup(IFormEx parent,SearchDataBase sdb)
            : this(parent,sdb, false) { }

        public Popup(IFormEx parent, SearchDataBase sdb, bool singleMode)
            : this(parent)
        {
            applySd = sdb;
            this.singleMode = singleMode;
        }

        public void LoadStatus()
        {
            surface.Statuses = Core.StatusManager.ExtractXStatuses(applySd);
        }

        bool singleMode = false;
        public bool SingleMode
        {
            get { return singleMode; }
            set
            {
                singleMode = value;
            }
        }

        private void Popup_Shown(object sender, EventArgs e)
        {
            try
            {
                LoadStatus();
                if (singleMode)
                {
                    if (surface.Statuses.Length > 0)
                        surface.SelectedIndex = 0;
                    this.Refresh();
                    for (int i = 0; i < 100 && surface.ActualHeight == 0; i++)
                    {
                        Application.DoEvents();
                        System.Threading.Thread.Sleep(10);
                    }
                    this.Height = (int)surface.ActualHeight;
                    surface.InitScrollbar();
                    var wa = Screen.GetWorkingArea(this).Height;
                    if (this.Location.Y + this.Height > wa)
                    {
                        this.Top = wa - this.Height;
                    }
                }
                this.Activate();
            }
            catch (ObjectDisposedException) { }
        }

        public Status SelectedStatus
        {
            get { return surface.SelectedStatus.BaseStatus; }
        }

        public void RefreshTL(bool remeasure)
        {
            surface.Refresh(remeasure);
        }

        public void RefreshTLWeak(bool remeasure)
        {
            surface.RefreshWeak(remeasure);
        }

        public void EnsureVisible()
        {
            surface.EnsureVisible();
        }

        public void SetCursor(Cursor cursor)
        {
            this.Cursor = cursor;
        }

        public Krile.Forms.MainForm.Controls.XTab.XTabWindowItem LinkingWindowTab
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        public void EnsureVisibleTop()
        {
            surface.EnsureVisibleTop();
        }

        public void CreateTab(string name, SearchDataCluster cluster, bool unNotify, string[] svids)
        {
            parentForm.CreateTab(name, cluster, unNotify, svids);
        }

        public void RegistTab(Krile.Forms.MainForm.Controls.XTab.XTabItem item)
        {
            parentForm.RegistTab(item);
        }

        public void RemoveTab(Krile.Forms.MainForm.Controls.XTab.XTabItem item)
        {
            throw new NotSupportedException();
        }

        public void DeselectStatus()
        {
            surface.SelectedIndex = -1;
        }

        public void AddInheritedTabAutoFormat(string extractSearchName, SearchDataBase[] addSearchDatas)
        {
            parentForm.AddInheritedTabAutoFormat(extractSearchName, addSearchDatas);
        }

        public void AddInheritedTab(string name, SearchDataBase[] addSearchDatas)
        {
            parentForm.AddInheritedTab(name, addSearchDatas);
        }

        public bool SetSelectStatus(SearchTarget.SelectTargets target)
        {
            throw new NotSupportedException();
        }

        public void SetScrollState(SearchTarget.SelectTargets target)
        {
            throw new NotSupportedException();
        }

        public Krile.Forms.MainForm.Controls.XTab.XTab TabControl
        {
            get { throw new NotSupportedException(); }
        }

        public Krile.Forms.MainForm.Controls.Surface.Surface Surface
        {
            get { return surface; }
        }

        public void UpdateTimeline()
        {
            LoadStatus();
        }

        public void SetReadedAll()
        {
            surface.DoEnumOperationWithProtectedThread((a) => a.Unread = false);
            surface.Refresh();
        }

        public bool SelectStatusByUID(ulong uid, bool p)
        {
            return surface.SelectStatusByUID(uid, p);
        }

        public void SetSearchbarVisible(bool visible)
        {
            throw new NotSupportedException();
        }

        public bool SearchbarVisible
        {
            get { return false; }
        }

        public HandleRef hWndHandle
        {
            get { return new HandleRef(this, this.Handle); }
        }
    }
}
